package oas.web;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import oas.domain.User;

/**
 * header导航栏的simpletag
 *
 * @author wjy
 * @version v1.1.0
 */
public class NavigationSimpleTag extends SimpleTagSupport {

    private String contextURL = null;

    @Override
    public void doTag() throws JspException, IOException {

        HttpSession session = ((PageContext) this.getJspContext()).getSession();
        JspWriter out = getJspContext().getOut();
        HttpServletRequest req = (HttpServletRequest) ((PageContext) this.getJspContext()).getRequest();
        contextURL = req.getContextPath();

        StringBuilder outMsg = new StringBuilder();
        outMsg.append(PART_0);
        outMsg.append("<a class=\"navbar-brand\" href=\"").append(contextURL).append("\">线上考勤系统</a> \n</div> \n<ul class=\"navbar-nav\"> \n");
        User user = (User) session.getAttribute("user");
        if (user != null) {
            outMsg.append("<li class=\"nav-item\"><span class=\"navbar-text\">" + "您好，").append(user.getUsername()).append("</span></li> \n");
            outMsg.append("<li class=\"nav-item\"><a class=\"nav-link\" href=\"").append(contextURL).append("/user/center\">个人信息 </a></li> \n");
            outMsg.append("<li class=\"nav-item\"><a class=\"nav-link\" href=\"").append(contextURL).append("/logout\">注销 </a></li> \n");
        } else {
            outMsg.append("<li class=\"nav-item\"><a class=\"nav-link\" href=\"").append(contextURL).append("/login\">登录 </a></li> \n");
        }
        outMsg.append("</ul> \n</div> \n</nav> \n");

        try {
            out.write(outMsg.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static final String PART_0 = "<nav class=\"navbar bg-dark navbar-dark navbar-expand-sm fixed-top\" role=\"navigation\">\n" +
            "<div class=\"container-fluid\"> \n" +
            "<div class=\"navbar-header\"> \n";

}
